//
// File: game.cpp
// Description: 
//

#include <iostream>
#include <GL\glfw.h>

#include "game.h"
#include "gui.h"
#include "logingui.h"
#include "chatgui.h"
#include <string>
#include <list>
#include <cmath>
using namespace std;

GUI* gui; // singleton
ImageHandler* ih;
Bar* testBar = 0;	
ButtonGroup* group = 0;
ToggleButton* toggle1 = 0;	
ToggleButton* toggle2 = 0;	
ToggleButton* toggle3 = 0;
ToggleButton* toggle4 = 0;	
ToggleButton* toggle5 = 0;	
	
void GLFWCALL resize(int width, int height) {
	glViewport(0,0,width,height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0, width, height, 0);
	glMatrixMode(GL_MODELVIEW);
}

void GLFWCALL key_callback(int key, int state) {
	//~ cout << "Key: " << key << "(" << (char)key << "), " << state << endl;
	// 288 = shift, 295 = backspace
	if ( key == GLFW_KEY_LSHIFT ) {
		GUI::getInstance()->lShiftHeld = ( state == GLFW_PRESS );
	} else if ( key == GLFW_KEY_RSHIFT ) {
		GUI::getInstance()->rShiftHeld = ( state == GLFW_PRESS );
	} else if ( key == GLFW_KEY_LCTRL ) {
		GUI::getInstance()->lCtrlHeld = ( state == GLFW_PRESS );
	} else if ( key == GLFW_KEY_RCTRL ) {
		GUI::getInstance()->rCtrlHeld = ( state == GLFW_PRESS );
	}
	
	
	GUI::getInstance()->handleKeyInput( key, state );
	
	
	
}
	
void GLFWCALL mousePosCallback( int mX, int mY ) {
	//~ cout << "X: " << mX << " Y: " << mY << endl;
	//~ if ( parent ) {
		//~ parent->setPosition( mX, mY );
	//~ }
	GUI::getInstance()->handleMouseMove( mX, mY );
}

void GLFWCALL mouseWheelCallback( int pos ) {
	static int lastPos = 0;
	int difference = pos - lastPos;
	if ( testBar != 0 ) {
		testBar->addPercentageFilled( difference / 100.0f );
	}
	GUI::getInstance()->handleMouseWheel( difference );
	lastPos = pos;
}

void GLFWCALL mouseClickCallback( int button, int state ) {
	//~ cout << "Mouse button ";
	//~ if ( state == GLFW_PRESS ) {
		//~ cout << "pressed: ";
	//~ } else {
		//~ cout << "released: ";
	//~ }
	//~ cout << button << " state: " << state << " ";
	//~ if ( button == GLFW_MOUSE_BUTTON_LEFT ) {
		//~ cout << "(left click)" << endl;
	//~ } else if ( button == GLFW_MOUSE_BUTTON_RIGHT ) {
		//~ cout << "(right click)" << endl;
	//~ } else if ( button == GLFW_MOUSE_BUTTON_MIDDLE ) {
		//~ cout << "(middle click)" << endl;
	//~ }
	
	GUI::getInstance()->handleMouseClick( button, state );
	
	
}
	

void GLFWCALL char_callback(int key, int state) {
	if (!state) {
		cout << "Press: " << (char)key << endl;
	}
}
	

void draw() {
	glClear(GL_COLOR_BUFFER_BIT);
}


int main() {
	
	glfwInit();
	
	glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, true);
	bool ok = glfwOpenWindow(
		SCREENW, SCREENH,
		8,8,8,
		8,
		24,
		0,
		GLFW_WINDOW
	);
	
	
	if (!ok) {
		glfwTerminate();
		return 0;
	}
	
	glfwSetWindowTitle("Buzz");
	glfwEnable(GLFW_STICKY_KEYS);
	
	glfwSetWindowSizeCallback(resize);
	glfwSetKeyCallback(key_callback);
	glfwSetMousePosCallback(mousePosCallback);
	glfwSetMouseButtonCallback(mouseClickCallback);
	glfwSetMouseWheelCallback(mouseWheelCallback);
	//~ glfwSetCharCallback(char_callback);
	
	glfwEnable( GLFW_KEY_REPEAT );
	
	glClearColor(0,0,0,0);
	glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	gui = GUI::getInstance();
	ih = ImageHandler::getInstance();
	ih->setWorkingTexture( ih->loadImage( GUI_DIRECTORY"rwscheme.png" ) );
	
	
	
	Container itemList( gui, true );
	itemList.setDraggable();
	itemList.setSize( 300, 500 );
	
	ScrollPanel scroll( &itemList );
	//~ scroll.setBackgroundColor( Color(1.0f), Color(0.0f, 1.0f) );
	scroll.setBorder( true );
	scroll.setResizable( true );
	scroll.setPosition( 5, 5 );
	scroll.setSize( 290, 400 );
	
	ContainerList cl( &scroll );
	cl.setBorder( true );
	cl.setWidth( 270 );
	cl.setPosition( 5, 5 );
	
	Container item1( &cl, true );
	item1.setSize( 250, 50 );
	item1.setPosition( 5, 5 );
	
	Container item2( &cl, true );
	item2.setSize( 250, 50 );
	item2.setPosition( 5, 54 );
	
	cl.finalize();
	
	//~ ChatGUI cg;
	//~ gui->setFocusedComponent( &cg );
	
	//~ LoginGUI lg;
	//~ LoginGUI lg2;
	//~ LoginGUI lg3;
	//~ LoginGUI lg4;
	//~ LoginGUI lg5;
	//~ lg2.setPosition( 10, 10 );
	//~ testBar = new Bar( &lg );
	//~ // testBar->setBorder( true );
	//~ testBar->setPercentageFilled( .56f );
	//~ testBar->setBackgroundColor( Color(), Color() ); // black background
	//~ testBar->setPosition( 5, 140 );
	//~ testBar->setSize( 250, 20 );
	//~ testBar->setDisplayPercent();

	//~ toggle1 = new ToggleButton( &lg, 12, 13 ); // radio
	//~ toggle2 = new ToggleButton( &lg, 12, 13 ); // radio
	//~ toggle3 = new ToggleButton( &lg, 12, 13 ); // radio
	//~ toggle4 = new ToggleButton( &lg, 14, 15 ); // checkbox
	//~ toggle5 = new ToggleButton( &lg, 14, 15 ); // checkbox
	//~ toggle2->setPosition( toggle1->getX() + toggle1->getWidth(), toggle1->getY() );
	//~ toggle3->setPosition( toggle2->getX() + toggle2->getWidth(), toggle1->getY() );
	//~ toggle4->setPosition( toggle2->getX(), toggle1->getY() + toggle1->getHeight() );
	//~ toggle5->setPosition( toggle3->getX(), toggle1->getY() + toggle1->getHeight() );
	
	//~ group = new ButtonGroup( &lg );
	//~ group->add( toggle1 );
	//~ group->add( toggle2 );
	//~ group->add( toggle3 );
	
	//~ gui->setFocusedComponent( &lg );
	
	
	//~ int num = 100;
	//~ Button* b[num];
	//~ for ( int i = 0; i < num; i ++ ) {
		//~ b[i] = new Button( &lg, "TEST" );
		//~ b[i]->setPosition( rand()%400, rand()%400 );
	//~ }
	//~ cg.setGUIStyle( 10, 11, 8, 9 );
	
	cout << "Width of 'Working string:' " << GUI::getInstance()->getFont()->widthOfString( "Working string:" ) << endl;
	cout << "Making image handler " << endl;
	bool running = true;
	while (running) {
		draw();
		gui->update();
		gui->draw();
		glfwSwapBuffers();
		running = glfwGetWindowParam(GLFW_OPENED);
	}
	
	glfwTerminate();
	
	return 0;
}
